﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

/// <summary>
/// The Common namespace.
/// </summary>
namespace WebChatApi.Common
{
    /// <summary>
    /// Class MessageTemplateAccessController.
    /// </summary>
    class MessageTemplateAccessController : SqlDataAccessController<MessageTemplate>
    {
        #region Constants

        /// <summary>
        /// The column_ value
        /// </summary>
        protected const string column_Value = "Value";

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="MessageTemplateAccessController" /> class.
        /// </summary>
        public MessageTemplateAccessController()
            : base(CommonFramework.SqlConnection)
        {
        }

        #endregion

        /// <summary>
        /// Converts the entity object.
        /// </summary>
        /// <param name="sqlDataReader">The SQL data reader.</param>
        /// <returns>MessageTemplate.</returns>
        protected override MessageTemplate ConvertEntityObject(SqlDataReader sqlDataReader)
        {
            if (!sqlDataReader.Read())
            {
                return null;
            }

            MessageTemplate result = new MessageTemplate();

            result.Key = sqlDataReader[column_Key].DBToString();
            result.CultureInfo = sqlDataReader[column_CultureInfo].DBToString();
            result.Content = sqlDataReader[column_Content].DBToString();

            if (sqlDataReader[column_Value].DBToString() != string.Empty)
            {
                result.Value = SerializeUtility.Instance.DeserializeFromXml<SerializableDictionary<string, string>>(sqlDataReader[column_Value].DBToString());
            }

            return result;
        }

        /// <summary>
        /// Gets the message template by key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="cultureInfo">The culture info.</param>
        /// <returns>MessageTemplate.</returns>
        /// <exception cref="Gbi.OperationFailureException">sp_GetMessageTemplateByKey</exception>
        /// <exception cref="OperationFailureException">sp_GetMessageTemplateByKey</exception>
        public MessageTemplate GetMessageTemplateByKey(string key, string cultureInfo)
        {
            const string spName = "sp_GetMessageTemplateByKey";
            SqlDataReader sqlDataReader = null;

            try
            {
                List<SqlParameter> parameters = new List<SqlParameter>();

                parameters.Add(this.GenerateSqlSpParameter(column_Key, key));
                parameters.Add(this.GenerateSqlSpParameter(column_CultureInfo, cultureInfo));

                sqlDataReader = databaseOperator.ExecuteReader(spName, parameters);

                return ConvertEntityObject(sqlDataReader);
            }
            catch (Exception ex)
            {
                throw new OperationFailureException("sp_GetMessageTemplateByKey", ex, key);
            }
            finally
            {
                if (sqlDataReader != null)
                {
                    sqlDataReader.Close();
                }
            }
        }

        /// <summary>
        /// Updates the message template by key.
        /// </summary>
        /// <param name="template">The template.</param>
        /// <exception cref="Gbi.OperationFailureException">sp_UpdateMessageTemplateByKey</exception>
        /// <exception cref="OperationFailureException">sp_UpdateMessageTemplateByKey</exception>
        public void UpdateMessageTemplateByKey(MessageTemplate template)
        {
            const string spName = "sp_UpdateMessageTemplateByKey";

            try
            {
                List<SqlParameter> parameters = new List<SqlParameter>();

                parameters.Add(this.GenerateSqlSpParameter(column_Key, template.Key));
                parameters.Add(this.GenerateSqlSpParameter(column_CultureInfo, template.CultureInfo));
                parameters.Add(this.GenerateSqlSpParameter(column_Content, template.Content));
                parameters.Add(this.GenerateSqlSpParameter(column_Value, template.Value == null ? null : SerializeUtility.Instance.SerializeToXml(template.Value)));

                databaseOperator.ExecuteScalar(spName, parameters);
            }
            catch (Exception ex)
            {
                throw new OperationFailureException("sp_UpdateMessageTemplateByKey", ex, template);
            }
        }
    }
}
